﻿/*
# -*- coding: utf-8 -*-
# 注意：

# 若希望将本代码用于商业用途，需先联系作者并获得明确授权。
# 作者不对因使用本代码引发的任何直接或间接损失负责。
# 如有违反上述条款的行为，作者保留追究法律责任的权利。

# 免责声明：
# 请尊重知识产权，遵循相关法律法规。

# Notice:
# If you wish to use this code for commercial purposes, you must contact the author and obtain explicit authorization.
# The author is not responsible for any direct or indirect losses caused by the use of this code.
# If any of the above terms are violated, the author reserves the right to pursue legal action.
# Disclaimer:
# Please respect intellectual property rights and comply with relevant laws and regulations.

# 注意：

# 若希望将本代码用于商业用途，需先联系作者并获得明确授权。
# 作者不对因使用本代码引发的任何直接或间接损失负责。
# 如有违反上述条款的行为，作者保留追究法律责任的权利。

# 免责声明：
# 请尊重知识产权，遵循相关法律法规。

# Notice:
# If you wish to use this code for commercial purposes, you must contact the author and obtain explicit authorization.
# The author is not responsible for any direct or indirect losses caused by the use of this code.
# If any of the above terms are violated, the author reserves the right to pursue legal action.
# Disclaimer:
# Please respect intellectual property rights and comply with relevant laws and regulations.

# 注意：

# 若希望将本代码用于商业用途，需先联系作者并获得明确授权。
# 作者不对因使用本代码引发的任何直接或间接损失负责。
# 如有违反上述条款的行为，作者保留追究法律责任的权利。

# 免责声明：
# 请尊重知识产权，遵循相关法律法规。

# Notice:
# If you wish to use this code for commercial purposes, you must contact the author and obtain explicit authorization.
# The author is not responsible for any direct or indirect losses caused by the use of this code.
# If any of the above terms are violated, the author reserves the right to pursue legal action.
# Disclaimer:
# Please respect intellectual property rights and comply with relevant laws and regulations.

# 注意：

# 若希望将本代码用于商业用途，需先联系作者并获得明确授权。
# 作者不对因使用本代码引发的任何直接或间接损失负责。
# 如有违反上述条款的行为，作者保留追究法律责任的权利。

# 免责声明：
# 请尊重知识产权，遵循相关法律法规。

# Notice:
# If you wish to use this code for commercial purposes, you must contact the author and obtain explicit authorization.
# The author is not responsible for any direct or indirect losses caused by the use of this code.
# If any of the above terms are violated, the author reserves the right to pursue legal action.
# Disclaimer:
# Please respect intellectual property rights and comply with relevant laws and regulations.

# 注意：

# 若希望将本代码用于商业用途，需先联系作者并获得明确授权。
# 作者不对因使用本代码引发的任何直接或间接损失负责。
# 如有违反上述条款的行为，作者保留追究法律责任的权利。

# 免责声明：
# 请尊重知识产权，遵循相关法律法规。

# Notice:
# If you wish to use this code for commercial purposes, you must contact the author and obtain explicit authorization.
# The author is not responsible for any direct or indirect losses caused by the use of this code.
# If any of the above terms are violated, the author reserves the right to pursue legal action.
# Disclaimer:
# Please respect intellectual property rights and comply with relevant laws and regulations.

# 注意：

# 若希望将本代码用于商业用途，需先联系作者并获得明确授权。
# 作者不对因使用本代码引发的任何直接或间接损失负责。
# 如有违反上述条款的行为，作者保留追究法律责任的权利。

# 免责声明：
# 请尊重知识产权，遵循相关法律法规。

# Notice:
# If you wish to use this code for commercial purposes, you must contact the author and obtain explicit authorization.
# The author is not responsible for any direct or indirect losses caused by the use of this code.
# If any of the above terms are violated, the author reserves the right to pursue legal action.
# Disclaimer:
# Please respect intellectual property rights and comply with relevant laws and regulations.

# 注意：

# 若希望将本代码用于商业用途，需先联系作者并获得明确授权。
# 作者不对因使用本代码引发的任何直接或间接损失负责。
# 如有违反上述条款的行为，作者保留追究法律责任的权利。

# 免责声明：
# 请尊重知识产权，遵循相关法律法规。

# Notice:
# If you wish to use this code for commercial purposes, you must contact the author and obtain explicit authorization.
# The author is not responsible for any direct or indirect losses caused by the use of this code.
# If any of the above terms are violated, the author reserves the right to pursue legal action.
# Disclaimer:
# Please respect intellectual property rights and comply with relevant laws and regulations.
*/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class MissionManager : SingletonMonoBehaviour<MissionManager>
{

    const string settingPath = "easysave2.txt";
    public List<Mission> Missions = new List<Mission>();
    public List<Mission> MissionsOrigin = new List<Mission>();

    const string missionKey = "mission";
    const string levelKey = "level";

    [HideInInspector]
    public int MissionCurrentID;
    [HideInInspector]
    public int LevelCurrentID;

    void Awake()
    {
        //Missions = CreateMission(MissionsOrigin);
    }

    List<Mission> CreateMission(List<Mission> origin)
    {
        List<Mission> result = new List<Mission>();

        for (int i = 0; i < origin.Count; i++)
        {
            Mission missionOrigin = origin[i];
            result.Add(new Mission());

            int index = 0;
            for (int j = 0; j < missionOrigin.levels.Count; j++)
            {
                Level level = missionOrigin.levels[j];
                for (int k = 0; k < 7; k++)
                {
                    Level newLevel = new Level();
                    newLevel.moveMode = (MoveMode)k;
                    newLevel.ID = index;
                    newLevel.MissionID = i;
                    newLevel.width = level.width;
                    newLevel.height = level.height;
                    index++;
                    result[i].levels.Add(newLevel);
                }
            }
            result[i].ID = i;
        }
        return result;
    }

    public List<Level> GetLevelFromCurrentMission()
    {
        var result = Missions[MissionCurrentID].levels;
        return result;
    }

    public List<Level> GetLevelFromMissionByMissionID(int missionId)
    {
        if (missionId < Missions.Count)
            return Missions[missionId].levels;
        return null;
    }

    public Level GetCurrentLevelData()
    {
        var levelList = Missions.Find(x => x.ID == MissionCurrentID).levels;
        return levelList.Find(x => x.ID == LevelCurrentID);
    }

    public void FinishLevel()
    {
        List<Level> levelList = GetLevelFromCurrentMission();
        if (LevelCurrentID < levelList.Count - 1)
        {
            SaveLevelState(LevelCurrentID + 1, MissionCurrentID, LevelState.IsOpen);
        }
        else
        {
            SaveMissionState(MissionCurrentID + 1, MissionState.IsOpen);
        }
    }

    public string GetStarMission(int missionID)
    {
        List<Level> levelList = GetLevelFromMissionByMissionID(missionID);
        int total = 0;
        if (levelList == null)
            return "";
        foreach (var item in levelList)
        {
            LevelState levelState = GetLevelState(item.ID, missionID);
            switch (levelState)
            {
                case LevelState.IsLock:
                    break;
                case LevelState.IsOpen:
                    break;
                case LevelState.IsOneStar:
                case LevelState.IsTwoStar:
                case LevelState.IsThreeStar:
                    total += ((int)levelState - 1);
                    break;
                default:
                    break;
            }


        }
        return total + "/" + levelList.Count * 3;
    }

    public void NextLevel()
    {
        List<Level> levelList = GetLevelFromCurrentMission();
        if (LevelCurrentID < levelList.Count - 1)
        {
            LevelCurrentID++;
        }
        else
        {
            MissionCurrentID++;
            LevelCurrentID = 0;
        }
    }

    public bool isWin()
    {
        if (Missions.Count - MissionCurrentID >= 2)
            return false;
        if (Missions[MissionCurrentID].levels.Count - LevelCurrentID >= 2)
            return false;
        return true;
    }



    public void SaveLevelState(int levelID, int missionID, LevelState state)
    {
        var levelState = GetLevelState(levelID, missionID);
        if (state < levelState) return;
        string key = missionID + "" + levelID;
        PlayerPrefs.SetInt(key, (int)state);
    }

    public LevelState GetLevelState(int levelID, int missionID)
    {
        string key = missionID + "" + levelID;
        int defaultValue = levelID == 0 ? (int)LevelState.IsOpen : (int)LevelState.IsLock;
        var value = PlayerPrefs.GetInt(key, defaultValue);

        return (LevelState)value;
    }


    public MissionState GetMissionState(int MissionID)
    {
        if (MissionID == 0 || MissionID == 1)
            return MissionState.IsOpen;
        string key = missionKey + MissionID;
        int defaultValue = MissionID == 0 || MissionID == 1 ? (int)LevelState.IsOpen : (int)LevelState.IsLock;
        try
        {
            var value = PlayerPrefs.GetInt(key, defaultValue);
            return (MissionState)value;
        }
        catch (System.Exception)
        {
            return MissionState.IsLock;
        }

    }

    public void SaveMissionState(int MissionID, MissionState state)
    {
        string key = missionKey + MissionID;
         PlayerPrefs.SetInt(key, (int)state);
    }


}
